__author__ = 'Vivienfanghua'
# -*- coding:utf-8 -*-
import urllib2
import json
import codecs
import socket
import sys
import datetime
reload(sys)
sys.setdefaultencoding('utf-8')

socket.setdefaulttimeout(10)
baseurl="http://service.envicloud.cn:8082/v2/weatherhistory/DML2AWVUZMFUZZE0OTCXOTIYNTI3MJU="
user_agent = 'Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)'
headers = { 'User-Agent' : user_agent }
a=codecs.open("C://Code/Python/testpython/Weather/shanghai.txt", "w", "utf8")
b=codecs.open("C://Code/Python/testpython/Weather/error_one.txt", "w", "utf8")
citystart=101020100
def getOne(url):
    try:
        request = urllib2.Request(url)
        response = urllib2.urlopen(request)
        content=response.read().decode("utf-8")
        js=json.loads(content)
        for i in js:
            a.write(str(js[i])+" ")
        a.write("\n")
        print(url)
    except Exception as e:
        b.write(url+"\n")

if __name__=="__main__":
    for i in range(11):
        city=citystart+i*100
        begin=datetime.date(2016,1,1)
        end=datetime.date(2017,6,10)
        for j in range((end-begin).days+1):
            day=begin+datetime.timedelta(j)
            url=baseurl+"/"+str(city)+"/"+day.strftime("%Y%m%d")
            getOne(url)
    a.close()
    b.close()

